﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ramparts
{
    
    
    public class TetrisPiece : GameComponent
    {

        public enum TETRIS_TILES { L_TILE=0, S_TILE, I_TILE, O_TILE, ONE_TILE};
        public enum TETRIS_COLORS { BLANK = 0, RED, GOLD, GREEN};
        SpriteSheet tetrisSheet;
        SceneManager sceneManager;
        Texture2D tetrisTexture;
        public int[,] currentBlock;

        // Tetris Templates
        
        public int[,] L_TILE = new int[,]{ { 0, 0 ,0 ,0 },
                                           { 1, 2 ,1 ,0 }, 
                                           { 0, 0 ,2 ,0 }, 
                                           { 0, 0 ,0 ,0 } };

        public int[,] S_TILE = new int[,]{ { 0, 1 ,0 ,0 },
                                           { 0, 3 ,1 ,0 }, 
                                           { 0, 0 ,3 ,0 }, 
                                           { 0, 0 ,0 ,0 } };

        public int[,] I_TILE = new int[,]{ { 0, 0 ,0 ,0 },
                                           { 0, 0 ,0 ,0 }, 
                                           { 1, 2 ,1 ,2 }, 
                                           { 0, 0 ,0 ,0 } };

        public int[,] O_TILE = new int[,]{ { 0, 0 ,0 ,0 },
                                           { 0, 2 ,2 ,0 }, 
                                           { 0, 2 ,2 ,0 }, 
                                           { 0, 0 ,0 ,0 } };

        public int[,] ONE_TILE = new int[,]{ { 0, 0 ,0 ,0 },
                                           { 0, 0 ,1 ,0 }, 
                                           { 0, 0 ,0 ,0 }, 
                                           { 0, 0 ,0 ,0 } };


        public TetrisPiece(SceneManager sceneManager, Vector2 position, int tile)
        {
            this.sceneManager = sceneManager;
            this.position = position;
            
            tetrisTexture = sceneManager.rampart.Content.Load<Texture2D>("Content\\tetris_tiles");
            tetrisSheet = new SpriteSheet(tetrisTexture);
            tetrisSheet.AddSourceSprite((int)TETRIS_COLORS.RED,
                new Rectangle(0, 0, 20, 20));
            tetrisSheet.AddSourceSprite((int)TETRIS_COLORS.GOLD,
                new Rectangle(20, 0, 20, 20));
            tetrisSheet.AddSourceSprite((int)TETRIS_COLORS.GREEN,
              new Rectangle(40, 0, 20, 20));


            switch ((TETRIS_TILES) tile)
            {
                case TETRIS_TILES.L_TILE:
                    currentBlock = L_TILE;
                    break;
                case TETRIS_TILES.S_TILE:
                    currentBlock = S_TILE;
                    break;
                case TETRIS_TILES.O_TILE:
                    currentBlock = O_TILE;
                    break;
                case TETRIS_TILES.I_TILE:
                    currentBlock = I_TILE;
                    break;
                case TETRIS_TILES.ONE_TILE:
                    currentBlock = ONE_TILE;
                    break;
                default:
                    currentBlock = L_TILE;
                    break;
            }
          
        }

        public void rotate()
        {
            int[,] tmpArray = new int[4,4];
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    tmpArray[x, y] = currentBlock[y , 3 - x];
                }
            }
            currentBlock = tmpArray;
        }

        public override void Draw()
        {
           
            Vector2 tilePosition = sceneManager.world.wallLayer.onTile(position);
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    if (x + tilePosition.X < sceneManager.world.wallLayer.gridWidth
                        && y + tilePosition.Y < sceneManager.world.wallLayer.gridHeight)
                    {

                        if (currentBlock[x, y] != 0)
                        {
                            Rectangle sourceRect = tetrisSheet.GetRectangle(currentBlock[x, y]);
                            sceneManager.rampart.spriteBatch.Draw(
                                  tetrisSheet.Texture,
                                  new Vector2((x + tilePosition.X) * 20, (y + tilePosition.Y) * 20),
                                  sourceRect,
                                  color,
                                  rotation,
                                  origin,
                                  scale,
                                  effects,
                                  layerDepth);
                        }
                    }
                }
            }
        }

    }
}
